package com.demo;

import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExchanerDemo {

	public static void main(String[] args) {

		Exchanger<String>  exchanger = new Exchanger<>();
		ExecutorService threadPool=Executors.newCachedThreadPool();
		//绑匪
		threadPool.execute(new Runnable() {
			
			@Override
			public void run() {
				try {
					String str = exchanger.exchange("小乔");
					System.out.println("绑架者用小乔交换回："+str);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
			}
		});
		
		//大乔
		threadPool.execute(new Runnable() {
		
		@Override
		public void run() {
			try {
				 long s = (long)(Math.random()*10000) ;
				 System.out.println("休眠====" +s);
				Thread.sleep( s);
				String str = exchanger.exchange("1000万");
				System.out.println("大乔用1000万交换回："+str);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
	});
		threadPool.shutdown();
	}

}
